/*
 * LevelLoader.cpp
 *
 *  Created on: Jan 27, 2014
 *      Author: GW
 */

#include "LevelLoader.h"
#include "Level.h"

#include "CatSpawnSet.h"
#include "CatSpawnScript.h"

#include "Supporting/Enums/EnumLevelType.h"
#include "Supporting/PlayerData.h"

using namespace cocos2d;

//===== CONSTRUCTOR =====
LevelLoader::LevelLoader()
{
	//_levelFileDirectory = new CCString("levelFileXMLDirectory.xml");
}
//===== DESTRUCTOR =======
LevelLoader::~LevelLoader()
{
	//delete _levelFileDirectory;
}

//===== FUNCTIONS =====

/*!
    @function 	getLevelList
    @brief		Get all light loaded level details for display in stage select screen
    @result		returns an array of all levels (light-loaded)
 */
Level** LevelLoader::getLevelList()
{
	//load the levels and store into levelList
	CCString levelsFile = CCFileUtils::sharedFileUtils()->fullPathForFilename("Game/Level/Levels.plist");

	CCArray *levelData = CCArray::createWithContentsOfFileThreadSafe(levelsFile.getCString());
	Level** levelList = new Level*[levelData->count()];

	CCLog("Number of levels: %d", levelData->count());

	for( int i = 0 ; i < levelData->count() ; i++ )
	{
		CCDictionary *individuallevelData = (CCDictionary*)levelData->objectAtIndex(i);

		int currLevelType = individuallevelData->valueForKey("levelType")->intValue();
		int currLevelNumber = individuallevelData->valueForKey("levelNumber")->intValue();
		CCString *levelname = CCString::create(individuallevelData->valueForKey("levelName")->getCString());
		bool currLevelCompleted = PlayerData::getLevelCompleted(currLevelNumber);
		int currLevelScore = PlayerData::getLevelScore(currLevelNumber);

		levelList[i] = new Level(currLevelType,currLevelCompleted,levelname,currLevelNumber,currLevelScore);
	}

	return levelList;
}


/*!
    @function 	getLevel
    @brief		Get a level based on the level number based in. LevelNumber should start from 1
    @param		levelNumber:int
				level number to retrieve, levelnumber should start from 1
    @result		returns a full load level
 */

Level* LevelLoader::getLevel(int levelNumber)
{
	//load the levels and store into levelList
	CCString levelsFile = CCFileUtils::sharedFileUtils()->fullPathForFilename("Game/Level/Levels.plist");

	CCArray *levelData = CCArray::createWithContentsOfFileThreadSafe(levelsFile.getCString());

	//levelNumber must minus 1 if the stage number does not start from 0
	CCDictionary *individualLevelData = (CCDictionary*)levelData->objectAtIndex(levelNumber-1);

	int levelType = individualLevelData->valueForKey("levelType")->intValue();

	//Array of spawn script. Each script is a CCDictionary
	CCArray* spawnScript = (CCArray*) individualLevelData->objectForKey("spawnScript");
	CCArray* spawnSetArray = new CCArray(); // deleted in CatSpawnScript

	if( spawnScript->count() > 0 )
	{
		for( int i = 0 ; i < spawnScript->count() ; i++ )
		{

			CCDictionary *spawnset = (CCDictionary*)spawnScript->objectAtIndex(i);

			int typeOfCat = spawnset->valueForKey("typeOfCat")->intValue();
			int spawnInterval = spawnset->valueForKey("spawnInterval")->intValue();
			int prespawnDelay = spawnset->valueForKey("prespawnDelay")->intValue();
			int numberToSpawn = spawnset->valueForKey("numberToSpawn")->intValue();

			spawnSetArray->addObject(new CatSpawnSet(typeOfCat,spawnInterval,numberToSpawn,prespawnDelay));
		}
	}

	CatSpawnScript *newSpawnScript = new CatSpawnScript(spawnSetArray); // Deleted in Level

	CCString* currLevelName = CCString::create(individualLevelData->valueForKey("levelName")->getCString());

	int currLevelDuration = individualLevelData->valueForKey("levelDuration")->intValue();
	int currLevelNumber = individualLevelData->valueForKey("levelNumber")->intValue();
	int currMaxDiff = individualLevelData->valueForKey("maxDifficulty")->intValue();
	int curMinDiff = individualLevelData->valueForKey("minDifficulty")->intValue();
	int currNumOfObstacles = individualLevelData->valueForKey("numOfObstacles")->intValue();
	int currMapWidth = individualLevelData->valueForKey("mapWidth")->intValue();
	bool currLevelCompleted = PlayerData::getLevelCompleted(currLevelNumber);
	int currLevelScore = PlayerData::getLevelScore(currLevelNumber);

	CCLog("leveltype: %d", levelType);
	CCLog("levelName: %s", currLevelName->getCString());
	CCLog("currLevelNumber: %d", currLevelNumber);
	CCLog("levelscore: %d", currLevelScore);
	CCLog("levelcompleted: %d", currLevelCompleted);
	CCLog("maxdifficulty: %d", currMaxDiff);
	CCLog("mindifficulty: %d", curMinDiff);
	CCLog("numofobstacles: %d", currNumOfObstacles);
	CCLog("mapwidth: %d", currMapWidth);

	Level *leveldata = new Level(levelType, newSpawnScript, currLevelDuration, currLevelCompleted,currLevelNumber,currLevelScore,
			curMinDiff,currMaxDiff,currNumOfObstacles,currMapWidth, currLevelName);

	return leveldata;
}
